<?php

/**
 * 角色模型
 *
 * @author haifeng.wu
 */
class Application_Model_Role extends Application_Model_Common
{
    /**
     * 表名
     *
     * @var string
     */
    protected $_name = 'front_role';

    /**
     * 构造函数
     * 
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 检查角色是否存在用户
     * 
     * @param string $name
     * @param 1|2 $type
     * @param string $parentRoleCode
     * @param integer $id
     * @return integer
     */
    public function isExists($name, $type, $parentRoleCode, $id = 0)
    {
        $wheres = array();
        $wheres[] = "`name` = {$this->_db->quote($name)}";
        if ($type == 1) {
            $wheres[] = "`type` = 1";
        } else {
            $wheres[] = "`type` = 2 AND `parentRoleCode` = {$this->_db->quote($parentRoleCode)}";
        }
        if ($id != 0) {
            $wheres[] = "`id` <> {$id}";
        }

        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT count(*) AS c FROM `{$this->getTableName()}` {$where}";
        $row = $this->_db->fetchRow($sql);
        $isExists = $row['c'] > 0 ? 1 : 0;
        return $isExists;  
    }


}